// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    try {
        const wxContext = cloud.getWXContext()

        const {
            account,
            password
        } = event

        // 查询是否登录过
        const result = await db.collection('user').where({
            _openid: wxContext.OPENID,
            account,
            password,
        }).get()

        if (result.data.length > 0) {
            // 直接登录
            return {
                code: 200,
                data: result.data[0],
                msg: "登录成功",
            }
        } else {
            return {
                code: 401,
                msg: "请注册账号",
            }
        }
    } catch {
        return {
            code: 500,
            msg: "登录失败",
        }
    }
}